package com.oocl.o2o.merchant.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.o2o.merchant.entity.Merchant;
import com.oocl.o2o.merchant.service.MerchantManager;

@Controller
public class LoginController {
	@Autowired
	private MerchantManager merchantManager;

	@PostMapping("login.action")
	@ResponseBody
	public String login(String username, String password, HttpSession session) {
		Merchant merchant = merchantManager.login(username, password);
		if (merchant == null) {
			return "fail";
		}
		session.setAttribute("merchantId", merchant.getId());
		session.setAttribute("username", merchant.getUsername());
		return "success";
	}
	
	@GetMapping("/sec/exit.action")
	public String exit(HttpSession session) {
		session.invalidate();
		return "redirect:/login";
	}
}
